var app = getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchant: null,
    codes: '',
    agentType:0,
    agentUrls: "/api/memberAgent/isApplyAgent.do",
    action: "/api/memberAgent/agentLocalSave.do",
    submit: "{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      merchant: wx.getStorageSync("banrdId")
    });
    var $this=this;
    var pp = { banrdId: parseInt(wx.getStorageSync("banrdId"))};
    ux.request($this.data.agentUrls, pp, function (protocol) {
      $this.setData({
        agentType: protocol.data.agentType
      })
    }, true);
  },

  onForm: function (event) {
    if (this.data.submit) {
      var $this = this;
      var dataset = event.currentTarget.dataset;
      ux.request(this.data.submit, detail.value, function (protocol) {
        if (protocol.success) {
          if (dataset.url) {
            wx.redirectTo(dataset.url);
          }
        }
      }, true);
    }
  },

  onTap: function (event) {
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val) {
      data[dataset.key] = dataset.val;
    }
    this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  tijiao: function () {
    if(this.data.agentType==1){
      this.toast("您已是代理，无法在绑定代理卡");
      return false;
    }
    var $this = this;
    var parmas = {
      banrdId: this.data.merchant,
      codes: this.data.codes
    }
    wx.showLoading({
      title: '请稍等...',
    })
    console.log(JSON.stringify(parmas));
    ux.request(this.data.action, parmas, function (protocol) {


      if (protocol.data.type == 1) {
        $this.toast(protocol.data.msg);
        setTimeout(function () {
          var pages = getCurrentPages();
          var currPage = pages[pages.length - 1]; //当前页面
          var prevPage = pages[pages.length - 2]; //上一个页面
          wx.navigateBack({
            delta: 1
          })
        }, 1500);
        wx.hideLoading();
      } else {

        $this.toast(protocol.data.msg);
        wx.hideLoading();
      }
    });
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
  codes: function (e) {
    this.setData({
      codes: e.detail.value
    })
  },
  saosao: function () {
    wx.scanCode({
      success: (res) => {
        console.log("扫码结果");
        console.log(res);
        this.setData({
          codes: res.result
        })
      },
      fail: (res) => {
        console.log(res);
      }
    })
  }
})
